package com.bailian.app.controller.Apis;

import com.bailian.common.annotation.Anonymous;
import com.bailian.common.core.controller.BaseController;
import com.bailian.common.core.domain.AjaxResult;
import com.bailian.common.core.page.TableDataInfo;
import com.bailian.system.domain.BlWarehouse;
import com.bailian.system.domain.BlWarehouseType;
import com.bailian.system.domain.BlParkTags;
import com.bailian.system.domain.vo.BLWareHouseVo;
import com.bailian.system.service.IBlWarehouseService;
import com.bailian.system.service.IBlWarehouseTypeService;
import com.bailian.system.service.IBlParkTagsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库控制器
 * 
 * @author bailian
 */
@Api(tags = "仓库相关接口")
@RestController
@RequestMapping("/app/api/warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private IBlWarehouseService warehouseService;
    
    @Autowired
    private IBlWarehouseTypeService blWarehouseTypeService;
    
    @Autowired
    private IBlParkTagsService parkTagsService;

    /**
     * 查询仓库类型列表
     */
    @Anonymous
    @ApiOperation("查询仓库类型列表")
    @GetMapping("/types")
    public TableDataInfo getWarehouseTypes(
            @ApiParam("类型名称") @RequestParam(required = false) String typeName,
            @ApiParam("是否首页显示：1=显示，0=不显示") @RequestParam(required = false) Integer isShowHome) {
        startPage();
        
        BlWarehouseType blWarehouseType = new BlWarehouseType();
        if (typeName != null && !typeName.isEmpty()) {
            blWarehouseType.setTypeName(typeName);
        }
        if (isShowHome != null) {
            blWarehouseType.setIsShowHome(isShowHome);
        }
        
        List<BlWarehouseType> list = blWarehouseTypeService.selectBlWarehouseTypeList(blWarehouseType);
        return getDataTable(list);
    }

    /**
     * 获取仓库行业
     */
    @Anonymous
    @ApiOperation("获取仓库行业")
    @GetMapping("/tags")
    public AjaxResult getWarehouseListTags() {
        BlParkTags tags = new BlParkTags();
        tags.setTagType(2);
        List<BlParkTags> list = parkTagsService.selectBlParkTagsList(tags);
        return success(list);
    }

    /**
     * 获取仓库列表
     */
    @Anonymous
    @ApiOperation("获取仓库列表")
    @GetMapping("/list")
    public TableDataInfo getWarehouseList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("搜索关键词") @RequestParam(required = false) String keyword,
            @ApiParam("园区ID") @RequestParam(required = false) Long parkId,
            @ApiParam("是否推荐") @RequestParam(required = false) Integer isRecommend,
            @ApiParam("仓库类型") @RequestParam(required = false) Long warehouseType,
            @ApiParam("区域ID") @RequestParam(required = false) Long parkAreaId,
            @ApiParam("行业ID") @RequestParam(required = false) Long industryDataId,
            @ApiParam("仓库标签") @RequestParam(required = false) Long tagId,
            @ApiParam("是否支持Vr：1=支持，0=不支持") @RequestParam(required = false) Integer isVr) {
        
        startPage();

        BlWarehouse warehouse = new BlWarehouse();
        if (keyword != null && !keyword.isEmpty()) {
            warehouse.setWarehouseName(keyword);
        }
        if (parkId != null) {
            warehouse.setParkId(parkId);
        }
        if (parkAreaId != null) {
            warehouse.setParkAreaId(parkAreaId);
        }
        if (isRecommend != null) {
            warehouse.setIsRecommend(isRecommend);
        }
        if (warehouseType != null) {
            warehouse.setWarehouseType(warehouseType);
        }
        if (industryDataId != null) {
            warehouse.setIndustryDataId(industryDataId);
        }
        if (tagId != null) {
            warehouse.setTagId(tagId);
        }
        if (isVr != null) {
            warehouse.setIsVr(isVr);
        }
        
        List<BLWareHouseVo> list = warehouseService.selectBlWarehouseVoList(warehouse);

        return getDataTable(list);
    }
    
    /**
     * 获取仓库详情
     */
    @Anonymous
    @ApiOperation("获取仓库详情")
    @GetMapping("/{id}")
    public AjaxResult getWarehouseDetail(@ApiParam("仓库ID") @PathVariable Long id) {
        try {
            BLWareHouseVo warehouse = warehouseService.selectBlWarehouseVoByWarehouseId(id);
            if (warehouse == null) {
                return error("仓库不存在");
            }
            return success(warehouse);
        } catch (Exception e) {
            return error("获取仓库详情失败：" + e.getMessage());
        }
    }
} 